/*
** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/

#ifndef lstate_h
#define lstate_h

#include "lua.h"

#include "lobject.h"


typedef struct stringtable
{
	GCObject **hash;
	lu_int32 nuse;	/* number of elements */
	int size;
}stringtable;


/*
** `global state', shared by all threads of this state
*/
typedef struct global_State {
	// TODO 补充global_State成员变量
	stringtable strt;	/* hash table for strings */
	lua_Alloc frealloc;	/* function to reallocate memory */
	void *ud;	/* auxiliary data to `frealloc' */
	lu_byte currentwhite;
	lu_byte gcstate;	/* state of garbage collector */
	lu_mem totalbytes;	/* number of bytes currently allocated */
}global_State;


/*
**	per thread's state
*/
struct lua_State {
	CommonHeader;
	global_State *l_G;
	//TODO 补充lua_State成员变量
};


#define G(L)	(L->l_G)


/*
** Union of all collectable objects
*/
union GCObject{
	GCHeader gch;
	union TString ts;
	union Udata u;
	union Closure cl;
	struct Table h;
	struct Proto p;
	struct UpVal uv;
	struct lua_State th;  /* thread */
};


/* macros to convert a GCObject into a specific value */
#define rawgco2ts(o)	check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
#define gco2ts(o)	(&rawgco2ts(o)->tsv)

/* macro to convert any Lua object into a GCObject */
#define obj2gco(v)	(cast(GCObject *, (v)))


#endif
